package com.hnevc.recyclerviewdemo;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Created by Administrator on 2017/11/24.
 */

public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    String[] datas ;//传递过来的文字数组
    TypedArray images;//传递过来的图片数组
    Context mContext;//传递过来的上下文对象

    public MyAdapter(String[] datas, TypedArray images, Context mContext) {
        this.datas = datas;
        this.images = images;
        this.mContext = mContext;
    }

    //创建要显示的ViewHolder对象
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //根据layout文件，创建视图
        View itemView = LayoutInflater.from(mContext).inflate(R.layout.item_layout, null);
        //根据视图获取viewHolder
        MyViewHolder viewHolder = new MyViewHolder(itemView);

        return viewHolder;
    }
    //根据每一项数据，确定每一个列表项显示的外观
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        //将我们的ViewHolder转换为MyViewHolder
        MyViewHolder myViewHolder = (MyViewHolder) holder;
        //获取datas[position]的值，作为TextVie的文本显示
        myViewHolder.textView.setText(datas[position]);
        //myViewHolder.imageView.setImageDrawable(images.getDrawable(position));
        myViewHolder.imageView.setImageResource(R.drawable.a1);

    }

    //返回RecyclerView控件需要显示的数据项数量
    @Override
    public int getItemCount() {

        return datas.length;

    }
    //创建一个ViewHolder的内部类
    class MyViewHolder extends RecyclerView.ViewHolder{
        ImageView imageView;//显示图片控件
        TextView textView;//显示文本的控件
        public MyViewHolder(View itemView/*item视图*/) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.id_iv_pic);
            textView = (TextView) itemView.findViewById(R.id.id_tv_data);
        }
    }
}
